<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { BaseButton } from '@/components/Button'
import { Delete } from '@element-plus/icons-vue'
import {
  querySearchAsync,
  handleSelect,
  setContentType,
  restaurants,
  delCommonUsage,
  saveCommonUsage
} from '@/utils/commonUsages'
const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const formSchema = ref<FormSchema[]>([
  {
    field: 'name',
    label: t('role.roleName'),
    component: 'Input'
  },
  {
    field: 'roleType',
    label: '角色类型',
    component: 'Autocomplete',
    componentProps: {
      placeholder: '快捷存储与选择角色类型',
      fetchSuggestions: querySearchAsync,
      clearable: false,

      on: {
        select: handleSelect,
        focus: () => {
          setContentType(6)
        }
      },
      slots: {
        default: ({ item }: any) => {
          return (
            <div class="w-full flex justify-between">
              <span>{item.content}</span>
              <BaseButton
                type="danger"
                link
                icon={Delete}
                onClick={($event) => {
                  $event.stopPropagation()
                  $event.preventDefault()
                  delCommonUsage(item.id)
                }}
              ></BaseButton>
            </div>
          )
        },
        suffix: () => {
          return (
            <>
              <BaseButton type="primary" onClick={() => save()}>
                保存
              </BaseButton>
            </>
          )
        }
      }
    }
  },
  {
    field: 'coment',
    label: '描述信息',
    component: 'Input'
  },
  {
    field: 'sortOrder',
    label: '显示排序',
    value: 1,
    component: 'InputNumber',
    componentProps: {
      min: 0
    }
  }
])

const rules = reactive({
  name: [required()],
  roleType: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    if (formData.roleType) {
      await save()
    }
    if (restaurants.length) {
      const commonInfo: any = restaurants.find((item: any) => item.content === formData.roleType)
      if (commonInfo) {
        formData.roleTypeId = commonInfo.id
      }
    }
    console.log(formData, restaurants)
    return formData
  }
}

/** 保存快捷存储 */
const save = async () => {
  const formData = await getFormData()
  if (!formData.roleType) {
    return
  }
  saveCommonUsage(formData.roleType)
  // console.log(formData, formData.roleType)
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
